<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:11
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;
use app\common\model\Combine as Combine_Model;
use app\common\model\Project;
use app\common\model\CombineGoods;
use app\common\model\Goods;

/**
 * 组合下单
 * Class Combine
 * @package app\manage\admin
 */
class Combine extends Admin
{

    /**
     * 组合下单列表
     * @return mixed
     * @throws \think\Exception
     */
    public function index(){
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = Combine_Model::where($map)->order($order)->paginate();

        foreach ($data_list as $k=>$v)
        {
            $data_list[$k]['block_type'] = $v['block_type']==1?"点钟":$v['block_type']==2?"排钟":$v['block_type']==3?"call钟":$v['block_type']==4?"call钟":"选钟";
        }


        // 分页数据
        $page = $data_list->render();

        $btn_add = [
            'title' => '新增组合下单',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];

        $btn_edit = [
            'title' => '修改组合下单',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-info btn-sm',
            'href' => url('edit',['id'=>"__id__"]),
        ];

        $btn_item = [
            'title' => '添加包含的商品',
            'icon' => 'fa fa-fw fa-scribd',
            'class' => 'btn btn-info btn-sm',
            'href' => url('addGoods',['id'=>"__id__"]),
        ];

        $btn_item_list = [
            'title' => '查看包含的商品',
            'icon' => 'fa fa-fw fa-sitemap',
            'class' => 'btn btn-info btn-sm',
            'href' => url('goodsLists',['id'=>"__id__"]),
        ];

        //设置页面
        return ZBuilder::make('table')
            ->addColumn('id', 'ID', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('name', '组合名', 'text')
            ->addColumn('block_type', '上钟类型', 'text')
            ->addColumn('right_button', '操作', 'btn')
            ->addTopButton('add', $btn_add)// 添加顶部按钮
            ->addRightButton('edit', $btn_edit)// 添加顶部按钮
            ->addRightButton('item', $btn_item)// 添加顶部按钮
            ->addRightButton('lists', $btn_item_list)// 添加顶部按钮
            ->addTopButton('delete')
            ->setTableName('qt_data_combine')
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('套餐列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    /**
     * 添加套餐
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            $result = $this->validate($data, 'Combine');

            if ($result!=true)$this->error($result);

            $project_info = Project::get($data['project_id']);

            $data['project_name'] = $project_info['name'];

            $info = Combine_Model::get(['name'=>$data['name']]);

            if ($info)$this->error('组合下单名已经存在');

            if ($page = Combine_Model::create($data)) {

                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        } else {

           $list =  Project::column('id,name');

            return ZBuilder::make('form')
                ->addFormItems([
                    ['text', 'sort','','','',['排序号:']],
                    ['text', 'name','','','',['组合下单名称名称:']],
                    ['select', 'project_id', '请选择项目:','',$list],
                    ['radio','block_type','上钟类型：','',[1=>'点钟',2=>'排钟',3=>'加钟',4=>'call钟',5=>'选钟']],
                    ['select','room_type','房间类型：','',[1=>'按摩-只显示按摩与副项目',2=>'足浴-只显示足浴与副项目',3=>'按摩与足浴-显示全部项目',4=>'其他-显示全部项目']],
                ])
                ->setPageTitle('添加组合下单')
                ->fetch();
        }
    }


    /**
     * 编辑套餐
     * @param null $id
     * @return mixed
     * @throws \think\Exception
     */
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'Combine');

            $project_info = Project::get($data['project_id']);

            $data['project_name'] = $project_info['name'];

            if (true !== $result) $this->error($result);

            if (Combine_Model::where('id',$id)->update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = Combine_Model::get($id);

            $list =  Project::column('id,name');

            return ZBuilder::make('form')
                ->addFormItems([
                    ['text', 'sort','','','',['排序号:']],
                    ['text', 'name','','','',['组合下单名称名称:']],
                    ['select', 'project_id', '请选择项目:','',$list],
                    ['radio','block_type','上钟类型：','',[1=>'点钟',2=>'排钟',3=>'加钟',4=>'call钟',5=>'选钟']],
                    ['select','room_type','房间类型：','',[1=>'按摩-只显示按摩与副项目',2=>'足浴-只显示足浴与副项目',3=>'按摩与足浴-显示全部项目',4=>'其他-显示全部项目']],

                ])
                ->setFormData($info)
                ->setPageTitle('编辑组合下单')
                ->fetch();
        }
    }

    /**
     * 添加套餐关联的商品或者项目
     * @param null $id
     * @return mixed
     * @throws \think\Exception
     */
    public function addGoods($id=null)
    {
        if ($this->request->isPost())
        {
            $data = $this->request->post();

            $Combine_goods = CombineGoods::get(['combine_id'=>$data['combine_id'],'goods_id'=>$data['goods_id']]);

            if ($Combine_goods)
            {
                $Combine_goods->number = $Combine_goods['number']+$data['number'];

                $result = $Combine_goods->save();
            }else{
                $goods_info = Goods::get($data['goods_id']);
                $data['goods_name'] = $goods_info['name'];
                $result = CombineGoods::create($data);
            }

            if ($result)
            {
                $this->success('操作成功','index');
            }else{
                $this->error('操作失败');
            }
        }

        $goods_list = Goods::column('id,name');


        return ZBuilder::make('form')
            ->addHidden('combine_id',$id)
            ->addSelect('goods_id', '请选择商品', '', $goods_list)
            ->addNumber('number', '数量', '', '', '0', '100')
            ->setFormData()
            ->setPageTitle('添加包含的商品')
            ->fetch();
    }

    /**
     * 包含商品列表
     * @param $id
     * @return mixed
     * @throws \think\Exception
     */
    public function goodsLists($id)
    {

        $list = CombineGoods::where('combine_id',$id)->select();

        $btn_change_number = [
            'title' => '修改数量',
            'icon' => 'fa fa-fw fa-minus',
            'class' => 'btn btn-info',
            'href' => url('changeNum',['id'=>"__id__"]),
        ];

        //设置页面
        return ZBuilder::make('table')
            ->addColumn('id', 'ID', 'text')
            ->addColumn('goods_name', '商品名', 'text')
            ->addColumn('number', '数量', 'text')
            ->addColumn('right_button','操作')
            ->setTableName('qt_data_combine_goods')// 指定数据表名
            ->addRightButton('changeNum', $btn_change_number)// 添加顶部按钮
            ->addTopButton('delete')
            ->setPrimaryKey('id')// 设置主键名为username
            ->setPageTitle('组合下单包含商品列表')
            ->setRowList($list)// 设置表格数据
            ->fetch();
    }

    /**
     * 修改商品数量
     * @param $id [商品ID]
     */
    public function changenum($id){
        if ($this->request->isPost())
        {
            $data = $this->request->post();

            $Combine_goods = CombineGoods::get(['id'=>$data['id'],'goods_id'=>$data['goods_id']]);

            if ($Combine_goods)
            {
                $Combine_goods->number = $data['number'];
                $result = $Combine_goods->save();
            }

            if ($result)
            {
                $this->success('操作成功','index');
            }else{
                $this->error('操作失败');
            }
        }

        $goods_list = Goods::column('id,name');
        $info = CombineGoods::get($id);
        return ZBuilder::make('form')
            ->addHidden('id',$id)
            ->addSelect('goods_id', '请选择商品', '', $goods_list,$info->goods_id)
            ->addNumber('number', '数量', '', '', '0', '100')
            ->setFormData($info)
            ->setPageTitle('添加包含的商品')
            ->fetch();
    }

}